package com.anolesoft.yr.mbus.base.mapper;

import com.anolesoft.yr.mbus.base.entity.BaseAddressBookDO;
import com.anolesoft.yr.mbus.base.entity.BaseContactDO;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.LocalDateTimeTypeHandler;

import java.util.List;

/**
 * @author zhj
 * @date 2020/9/12 16:54
 */
@Mapper
public interface BaseAddressBookMapper {
    @Insert("insert into base_address_book (" +
            "objuid," +
            "fac_code," +
            "type," +
            "target_uid," +
            "name," +
            "dept," +
            "post," +
            "phone," +
            "email," +
            "c_user_uid," +
            "c_time," +
            "m_user_uid," +
            "m_time," +
            "del_flag," +
            "remark" +
            ") values (" +
            "#{objuid,jdbcType=VARCHAR}," +
            "#{factoryCode,jdbcType=VARCHAR}," +
            "#{type,jdbcType=VARCHAR}," +
            "#{targetUid,jdbcType=VARCHAR}," +
            "#{name,jdbcType=VARCHAR}," +
            "#{dept,jdbcType=VARCHAR}," +
            "#{post,jdbcType=VARCHAR}," +
            "#{phone,jdbcType=VARCHAR}," +
            "#{email,jdbcType=VARCHAR}," +
            "#{createUserUid,jdbcType=VARCHAR}," +
            "now()," +
            "#{modifyUserUid,jdbcType=VARCHAR}," +
            "now()," +
            "0," +
            "#{remark,jdbcType=VARCHAR}" +
            ")")
    int insert(BaseAddressBookDO baseAddressBookDO);

    @Update("update base_address_book set " +
            "fac_code = #{factoryCode,jdbcType=VARCHAR}," +
            "type = #{type,jdbcType=VARCHAR}," +
            "target_uid = #{targetUid,jdbcType=VARCHAR}," +
            "name = #{name,jdbcType=VARCHAR}," +
            "dept = #{dept,jdbcType=VARCHAR}," +
            "post = #{post,jdbcType=VARCHAR}," +
            "phone = #{phone,jdbcType=VARCHAR}," +
            "email = #{email,jdbcType=VARCHAR}," +
            "m_user_uid = #{modifyUserUid,jdbcType=VARCHAR}," +
            "m_time = now()," +
            "remark = #{remark,jdbcType=VARCHAR} " +
            " where " +
            "objuid = #{objuid,jdbcType=VARCHAR}")
    int update(BaseAddressBookDO baseAddressBookDO);

    @Update("update base_address_book set del_flag = 1 where objuid = #{id,jdbcType=VARCHAR}")
    int delete(@Param("id") String id);


    @Results(id = "BaseAddressBookResultMap", value = {
            @Result(id = true, column = "objuid", property = "objuid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "fac_code", property = "factoryCode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "type", property = "type", jdbcType = JdbcType.VARCHAR),
            @Result(column = "target_uid", property = "targetUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "contact_person", property = "contactPerson", jdbcType = JdbcType.VARCHAR),
            @Result(column = "crm_address", property = "crmAddress", jdbcType = JdbcType.VARCHAR),
            @Result(column = "contact_number", property = "contactNumber", jdbcType = JdbcType.VARCHAR),
            @Result(column = "postcode", property = "postcode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "c_user_uid", property = "createUserUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "c_time", property = "createTime", typeHandler = LocalDateTimeTypeHandler.class),
            @Result(column = "m_user_uid", property = "modifyUserUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "m_time", property = "modifyTime", typeHandler = LocalDateTimeTypeHandler.class),
            @Result(column = "del_flag", property = "deleteFlag", jdbcType = JdbcType.BOOLEAN),
            @Result(column = "remark", property = "remark", jdbcType = JdbcType.VARCHAR)
    })
    @Select("select " +
            "objuid, fac_code, type, target_uid, name, dept, post, phone, email," +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from base_address_book where del_flag = 0 ")
    List<BaseAddressBookDO> selectAll();

    @ResultMap("BaseAddressBookResultMap")
    @Select("select " +
            "objuid, fac_code, type, target_uid, name, dept, post, phone, email," +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from base_address_book where del_flag = 0 and objuid = #{id,jdbcType=VARCHAR}")
    BaseAddressBookDO selectOneById(@Param("id") String id);

    @ResultMap("BaseAddressBookResultMap")
    @Select("select " +
            "objuid, fac_code, type, target_uid, name, dept, post, phone, email," +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from base_address_book where del_flag = 0 and target_uid = #{targetUid,jdbcType=VARCHAR}")
    List<BaseAddressBookDO> selectListByTargetUid(@Param("targetUid") String targetUid);
}
